﻿using AdminSystem.Application;
using AdminSystem.Domain;
using AdminSystem.Repositories;
using AdminSystem.WebApi.MedialRs.Commands.TracebilityCommand;
using AutoMapper;
using MediatR;

namespace AdminSystem.WebApi.MedialRs.CommandHandlers.Tracebilitys
{
    public class TracebilityCommandHandler : IRequestHandler<TracebilityCommand, ApiResult<object>>
    {
        public TracebilityCommandHandler(IRepository<Tracebility> repository,IMapper mapper)
        {
            Repository = repository;
            Mapper = mapper;
        }

        public IRepository<Tracebility> Repository { get; }
        public IMapper Mapper { get; }

        public Task<ApiResult<object>> Handle(TracebilityCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<object> result=new ApiResult<object>();
                result.statusCode = StatusCode.Success;
                result.Message= "添加成功";
                var entity = Mapper.Map<Tracebility>(request);
                int i = Repository.Insert(entity).Result;
                if (i <= 0)
                {
                    result.statusCode = StatusCode.FaieId;
                    result.Message = "添加失败";
                    return Task.FromResult(result);
                }
                return Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
